/*  
    NetSpeed (C) Raja Jamwal 2011, www.experiblog.co.cc

    Distributed under GNU GPL License

    NetSpeed, determines the realtime network speed through kernel network interfaces
    through ifconfig utility 
    Copyright (C) 2011  Raja Jamwal

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    -------Building instruction
    Using g++, in terminal type
    	
          g++ NetSpeed.c -o NetSpeed `pkg-config --cflags --libs gtk+-2.0` 

*/

#include <gtk/gtk.h>
#include <strings.h>
#include <cstring>
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <unistd.h>

using namespace std;

// execute command in terminal and read output

std::string exec(char* cmd) {
    FILE* pipe = popen(cmd, "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    std::string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
                result += buffer;
    }
    pclose(pipe);
    return result;
}

// filter string from ifconfig and convert bytes string to interger

unsigned int byt(const char * sp)
{
        // having some compilation problem with string.h
	// did this manually
	char * ln = (char*) malloc(std::strlen(sp+1));
	for(int i=0; i< (std::strlen(sp)-6); i++)
	{
		ln[i] = sp[7+i];
	}
	unsigned int num = atoi(ln);
	free(ln);
	return num;
}


void closeApp(GtkWidget *widget, gpointer data)
{
    gtk_main_quit();
}

GtkWidget *label1;
unsigned sp[20]; // saves the previous bytes received

// the main function does everything there is to

gboolean spd (gpointer data)
{

stringstream out;
string output = exec("ifconfig"); // get ifconfig output
stringstream ss;
ss << output.c_str();

char line[512];
int nuC = 0;

// process data
while(ss.getline(line, 512))
{
        stringstream rd; rd << line;
	string dump; rd >> dump;
        if(line[0]!=' ' && line[1]!=' ')
	{
	out << dump  << "  " ; // connection name
	}else{
			
		do{
	  	if(dump[0]=='R' && dump[1]=='X'){
		//cout << dump; 
		
		rd>> dump;
		if(dump[0]=='b'){
			out << " " << ((byt(dump.c_str())/1024)-sp[nuC])  << " KBps " << ((byt(dump.c_str())/1024)-sp[nuC])*8 << " Kbps" << endl;
			sp[nuC] = (byt(dump.c_str())/1024); // in KB
			nuC++;
			}

		}
		}while(rd>>dump);
	}
}

  // update or display information
  gtk_label_set_text( GTK_LABEL(label1), out.str().c_str());
  return TRUE;

}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *hbox;
    GtkWidget *vbox;
    GtkWidget *button;

    gtk_init(&argc, &argv);
    button  = gtk_button_new_with_label("Quit");

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_window_set_title(GTK_WINDOW(window), "NetSpeed (C) Raja");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);
    gtk_window_set_default_size(GTK_WINDOW(window), 50, 20);

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(closeApp), NULL);
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(closeApp), NULL);
    label1 = gtk_label_new("NetSpeed\n\n\n\nInitializing...");
    gtk_label_set_justify(GTK_LABEL(label1), GTK_JUSTIFY_LEFT);

    vbox = gtk_vbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(vbox), label1, TRUE, TRUE, 2);
    gtk_container_add(GTK_CONTAINER(window), vbox);
    gtk_widget_show_all(window);
    gint m_timer = g_timeout_add(1000, spd, NULL);
    gtk_main();

    return 0;
}

